package com.caiyigo.baihua.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.caiyigo.baihua.Const.Dictionary;
import com.caiyigo.baihua.dao.ZhuanyeMapper;
import com.caiyigo.baihua.dao.ZhuanyeMapperSql;
import com.caiyigo.baihua.dao.ZykmMapper;
import com.caiyigo.baihua.dao.ZykmMapperSql;
import com.caiyigo.baihua.dto.Tree;
import com.caiyigo.baihua.dto.ZykmDto;
import com.caiyigo.baihua.model.Kaoshikemu;
import com.caiyigo.baihua.model.SystemRes;
import com.caiyigo.baihua.model.Zykm;
import com.caiyigo.baihua.service.ZykmService;
import com.caiyigo.baihua.shiro.ShiroExt;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

@Transactional
@Service
public class ZykmServiceImpl implements ZykmService {
	@Resource(name = "zykmMapper")
	ZykmMapper zykmMapper;
	@Resource(name = "zhuanyeMapper")
	ZhuanyeMapper zhuanyeMapper;

	@Override
	public boolean deleteByPrimaryKey(Integer id) {
		Zykm zykm = zykmMapper.isExistSon(id);
		boolean i = false;
		if (null != zykm) {
			i = zykmMapper.deleteByPid(id) >= 0;
			i = zhuanyeMapper.deleteByPrimaryKey(zykm.getZyid()) > 0;
		}
		i = zykmMapper.deleteByPrimaryKey(id) > 0;
		return i;
	}

	@Override
	public int insert(Zykm record) {

		return zykmMapper.insert(record);
	}

	@Override
	public int insertSelective(Zykm record) {

		return zykmMapper.insertSelective(record);
	}

	@Override
	public Zykm selectByPrimaryKey(Integer id) {

		return zykmMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(Zykm record) {

		return zykmMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Zykm record) {

		return zykmMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<ZykmDto> selectByPid(Integer pid) {
		// TODO Auto-generated method stub
		return zykmMapper.selectByPid(pid);
	}

	@Override
	public List<ZykmDto> selectByPidIsNull() {
		return zykmMapper.selectByPidIsNull();
	}

	@Override
	public List<ZykmDto> listBySeq() {
		List<ZykmDto>list=zykmMapper.listBySeq();
		for (ZykmDto z : list) {
			z.setKmlxMc(Dictionary.KMLX.getName(z.getKmlx()));
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.caiyigo.zoo.service.ResService#getTree(java.lang.Integer, int,
	 * java.lang.Integer)
	 */
	public List<Tree> getTree(Integer pid, int type, Integer passId) {
		// 根据用户角色来获取 列表
		List<Tree> trees = new ArrayList<Tree>();

		for (ZykmDto res : getChild(pid)) {

			if (res.getId().equals(passId))
				continue;
			Tree tree = new Tree(res.getId(), res.getPid(), res.getZymc(),
					null, res, false);
			tree.children = getTree(res.getId(), type, passId);
			if (tree.children.size() > 0)
				tree.changeState();

			trees.add(tree);
		}

		return trees;
	}

	public List<ZykmDto> getChild(Integer pid) {
		if (null == pid) {
			return zykmMapper.selectByPidIsNull();
		} else {
			return zykmMapper.selectByPid(pid);
		}

	}

	@Override
	public List selectForCombox(ZykmDto dto, PageBounds pageBounds) {
		// TODO Auto-generated method stub
		return zykmMapperSql.selectForCombox(dto, pageBounds);
	}

	@Resource(name = "zykmMapperSql")
	ZykmMapperSql zykmMapperSql;

	@Override
	public int deleteByPid(Integer pid) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Zykm isExistSon(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List selectKemu(String zyid, Kaoshikemu dto, PageBounds pageBounds) {
		// TODO Auto-generated method stub
		return zykmMapperSql.selectKemu(zyid, dto, pageBounds);
	}

	@Override
	public List selectKemuByZyid(ZykmDto dto, PageBounds pageBounds) {
		// TODO Auto-generated method stub
		return zykmMapperSql.selectKemuByZyid(dto, pageBounds);
	}
}
